﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using SharpShare.Afp.Protocol.Security;

namespace SharpShare.Afp.Configuration {
    public class AfpUamConfigurationElement : ConfigurationElement {
        [ConfigurationProperty("type", IsRequired = true, IsKey = true)]
        [TypeConverter(typeof(TypeNameConverter))]
        public Type Type {
            get {
                return (Type)this["type"];
            }
            set {
                this["type"] = value;
            }
        }

        public IAfpUserAuthenticationMethod Create() {
            if (!typeof(IAfpUserAuthenticationMethod).IsAssignableFrom(this.Type)) {
                throw new InvalidOperationException(string.Format("Type {0} does not implement {1}.", this.Type, typeof(IAfpUserAuthenticationMethod)));
            }

            IAfpUserAuthenticationMethod method = (IAfpUserAuthenticationMethod)Activator.CreateInstance(this.Type);

            return method;
        }
    }
}
